﻿package plugin.specialActivity.game
{
    import com.core.*;
    import com.sh.game.util.*;
    import flash.display.*;
    import flash.utils.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import plugin.component.*;
    import plugin.specialActivity.*;
    import uiresource.specialActivity.*;

    public class ActivityBgLRGame extends ActivityBgLRUI
    {
        private var _timeArr:Array;
        private var curPageIndex:int = 0;
        private var timer:int = 0;
        private var _closeFunc:Function;
        private var pageDic:Dictionary;
        private var curPage:IPage;
        private var endTime:int = 0;
        public static var pageObj:Object = {shuangbei:ShuangBeiGame, libao:LibaoGame, jizhi:JiZhiGame, bossreward:BossRewardGame};

        public function ActivityBgLRGame(param1:Function)
        {
            this.pageDic = new Dictionary();
            this._closeFunc = param1;
            btn_close.clickHandler = new Handler(this.doClose);
            return;
        }// end function

        override protected function initializeView() : void
        {
            var _loc_1:* = Control.create(SpecialActivityControl, this);
            ModelProxy.addObserver("specialActivity", _loc_1);
            return;
        }// end function

        public function doClose() : void
        {
            this._closeFunc();
            this.activityTimerEnd();
            return;
        }// end function

        public function setTitle(param1:String) : void
        {
            txt_title.text = param1;
            return;
        }// end function

        public function initTimeMsg(param1:int) : void
        {
            var _loc_3:Object = null;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            this._timeArr = [];
            var _loc_2:* = ConfigDictionary.data.activitytime;
            for each (_loc_3 in _loc_2)
            {
                
                if (_loc_3.activetype == param1)
                {
                    switch(int(_loc_3.timetype))
                    {
                        case 1:
                        {
                            if (++ModelProxy.getValue("data", "openday") >= _loc_3.opentime && ++ModelProxy.getValue("data", "openday") <= _loc_3.overtime)
                            {
                                this._timeArr.push(_loc_3);
                                _loc_3.titleUrl = "gongce/" + _loc_3.page + ".png";
                            }
                            break;
                        }
                        case 2:
                        {
                            _loc_5 = int((ModelProxy.getValue("data", "servertime") + Number(getTimer())) / 1000);
                            if (_loc_5 >= _loc_3.opentimestr / 1000 && _loc_5 <= _loc_3.overtimestr / 1000)
                            {
                                this._timeArr.push(_loc_3);
                                _loc_3.titleUrl = "gongce/" + _loc_3.page + ".png";
                            }
                            break;
                        }
                        default:
                        {
                            break;
                        }
                    }
                }
            }
            this._timeArr.sortOn("index", Array.NUMERIC);
            if (this._timeArr.length == 0)
            {
                ModelProxy.change("mainui", "removeBtn", "gongce");
                this.doClose();
            }
            list.array = this._timeArr;
            list.renderHandler = new Handler(this.titleRender);
            return;
        }// end function

        private function titleRender(param1:Component, param2:int) : void
        {
            var _loc_3:UrlImage = null;
            var _loc_4:Button = null;
            if (param2 < list.length)
            {
                _loc_3 = param1.getChildByName("img_btnname") as UrlImage;
                if (_loc_3.url != param1.dataSource.titleUrl)
                {
                    _loc_3.url = param1.dataSource.titleUrl;
                }
                _loc_4 = param1.getChildByName("btn_bg") as Button;
                if (this.curPageIndex == param2)
                {
                    _loc_4.selected = true;
                    this.pageChange(param1.dataSource);
                    list.selectHandler = new Handler(this.doSelect);
                }
                else
                {
                    _loc_4.selected = false;
                }
            }
            return;
        }// end function

        private function doSelect(param1:int) : void
        {
            this.curPageIndex = param1;
            list.refresh();
            return;
        }// end function

        private function pageChange(param1:Object) : void
        {
            var _loc_2:Sprite = null;
            if (this.curPage != null)
            {
                _loc_2 = this.curPage as Sprite;
                _loc_2.parent.removeChild(_loc_2);
            }
            var _loc_3:* = this.pageDic[param1.page];
            if (_loc_3 == null)
            {
                _loc_3 = new pageObj[param1.page](param1);
            }
            _loc_2 = _loc_3 as Sprite;
            this.addChild(_loc_2);
            this.curPage = _loc_3;
            this.pageDic[param1.page] = _loc_3;
            this.checkCurPageTime(param1);
            this.curPage.refresh();
            return;
        }// end function

        private function checkCurPageTime(param1:Object) : void
        {
            var _loc_2:Object = null;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            switch(int(param1.timetype))
            {
                case 1:
                {
                    _loc_2 = ActivityUtil.getEndTimeAndTimeCount(param1.overtime);
                    this.checkTime(_loc_2.timeDis, _loc_2.endTime);
                    break;
                }
                case 2:
                {
                    _loc_3 = Number(ModelProxy.getValue("data", "servertime") + getTimer()) / 1000;
                    _loc_4 = param1.overtimestr / 1000 - _loc_3;
                    this.checkTime(_loc_4, param1.overtimestr / 1000);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        public function activityTimerStart() : void
        {
            if (this.timer == 0)
            {
                this.timer = setInterval(this.doTime, 1000);
            }
            return;
        }// end function

        public function activityTimerEnd() : void
        {
            if (this.timer > 0)
            {
                clearInterval(this.timer);
                this.timer = 0;
            }
            return;
        }// end function

        private function doTime() : void
        {
            var _loc_1:int = 0;
            var _loc_2:int = 0;
            if (this.curPage != null)
            {
                _loc_1 = Number(ModelProxy.getValue("data", "servertime") + getTimer()) / 1000;
                _loc_2 = this.endTime - _loc_1;
                this.checkTime(_loc_2);
            }
            return;
        }// end function

        public function checkTime(param1:int, param2:int = 0) : void
        {
            if (param1 > 0)
            {
                if (param2 > 0)
                {
                    this.endTime = param2;
                    this.activityTimerStart();
                }
                if (param1 > 86400)
                {
                    this.curPage.update(ActivityPublic.UPDATETIME, "活动剩余时间：" + TimeDataFormatUtil.getLeftTimeStr2(param1));
                }
                else
                {
                    this.curPage.update(ActivityPublic.UPDATETIME, "活动剩余时间：" + TimeDataFormatUtil.getLeftTime(param1));
                }
            }
            else
            {
                this.activityTimerEnd();
                this.curPage.update(ActivityPublic.UPDATETIME, "活动剩余时间：活动已结束");
            }
            return;
        }// end function

        public function update(param1:int, param2:Object) : void
        {
            if (this.visible == false)
            {
                return;
            }
            if (this.curPage != null)
            {
                this.curPage.update(param1, param2);
            }
            return;
        }// end function

    }
}
